package sc.math;

/** Vector2D provides operations for vectors in two-space. */
public final class Vector2D {

    /** Prevent public instantiation of Vector2D */
    private Vector2D() {
    	
    }
    
    /** Calculates magnitude */
    public static double magnitude(double[] v) {
        return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
    }
     
    /** Sets to a vector from x1, y1 to x2, y2 */
    public static void vector(double[] v, double x1, double y1, double x2, double y2) {
        v[0] = x2 - x1;
        v[1] = y2 - y1;
    }
    
    /** Sets length to n */
    public static void setLength(double[] v, double n) {
        double d = magnitude(v);
        v[0] = (float) (v[0] / d) * n;
        v[1] = (float) (v[1] / d) * n;
    }
    
    /** Fast clockwise 90 degrees rotation */
    public static void rotate90(double[] v) {
        double x = -v[0];
        v[0] = v[1];
        v[1] = x;
    }
    
    /** Fast clockwise 180 degrees rotation */
    public static void rotate180(double[] v) {
        v[0] = -v[0];
        v[1] = -v[1];
    }
    
    /** Fast clockwise 270 degree rotated */
    public static void rotate270(double[] v) {
        double x = v[0];
        v[0] = -v[1];
        v[1] = x;
    }
    
    /* Rotates theta radians */
    public static void rotate(double[] v, double theta) {
    	double c = Math.cos(theta);
    	double s = Math.sin(theta);
    	double x = v[0];
    	v[0] = c*x - s*v[1]; 
    	v[1] = s*x + c*v[1];
    }
    
    /** Multiple by scaler n */
    public static void multiple(double[] v, double n) {
        v[0] = v[0] * n;
        v[1] = v[1] * n;
    }
    
    /** Adds vector z to vector v */
    public static void add(double[] v, double[] z) {
    	v[0] = v[0] + z[0];
    	v[1] = v[1] + z[1];
    }
    
    /** Returns the dot product of the two vectors v and z */
    public static double dot(double[] v, double[] z) {
    	return v[0]*z[0] + v[1]*z[1];
    }
    
}
